// 护工助手 App JS

// 应用初始化
document.addEventListener('DOMContentLoaded', function() {
    console.log('护工助手应用已加载');
    
    // 更新顶部时间
    updateTime();
    setInterval(updateTime, 60000);
    
    // 绑定事件
    bindEvents();
    
    // 模拟通知轮播
    startNoticeCarousel();
});

// 更新顶部时间显示
function updateTime() {
    const now = new Date();
    const hours = now.getHours().toString().padStart(2, '0');
    const minutes = now.getMinutes().toString().padStart(2, '0');
    document.querySelector('.time').textContent = `${hours}:${minutes}`;
}

// 绑定事件
function bindEvents() {
    // 快捷功能点击事件
    const actionItems = document.querySelectorAll('.action-item');
    actionItems.forEach(item => {
        item.addEventListener('click', function() {
            const actionType = this.querySelector('span').textContent;
            handleAction(actionType);
        });
    });
    
    // 订单卡片查看详情
    const orderButtons = document.querySelectorAll('.order-card .btn-primary');
    orderButtons.forEach(btn => {
        btn.addEventListener('click', function() {
            // 获取当前订单信息
            const orderCard = this.closest('.order-card');
            const deptText = orderCard.querySelector('.order-dept').textContent;
            const patientText = orderCard.querySelector('.order-patient .value').textContent;
            
            showOrderDetail(deptText, patientText);
        });
    });
    
    // 待办事项点击
    const todoItems = document.querySelectorAll('.todo-item');
    todoItems.forEach(item => {
        item.addEventListener('click', function() {
            const todoTitle = this.querySelector('.todo-title').textContent;
            handleTodo(todoTitle);
        });
    });
}

// 处理快捷功能点击
function handleAction(actionType) {
    switch (actionType) {
        case '上岗签到':
            showToast('正在打开上岗签到...');
            // 这里可以添加跳转到签到页面的代码
            setTimeout(() => {
                window.location.href = 'signin.html';
            }, 500);
            break;
        case '接单':
            showToast('正在打开接单中心...');
            setTimeout(() => {
                window.location.href = 'new-orders.html';
            }, 500);
            break;
        case '培训学习':
            showToast('正在打开培训中心...');
            setTimeout(() => {
                window.location.href = 'training.html';
            }, 500);
            break;
        case '我的钱包':
            showToast('正在打开我的钱包...');
            setTimeout(() => {
                window.location.href = 'income.html';
            }, 500);
            break;
        default:
            console.log('未知操作类型:', actionType);
    }
}

// 处理待办事项点击
function handleTodo(todoTitle) {
    switch (todoTitle) {
        case '服务签到':
            showToast('正在跳转到服务签到...');
            setTimeout(() => {
                window.location.href = 'service-signin.html';
            }, 500);
            break;
        case '新订单通知':
            showToast('正在查看新订单...');
            setTimeout(() => {
                window.location.href = 'new-orders.html';
            }, 500);
            break;
        default:
            console.log('未知待办事项:', todoTitle);
    }
}

// 显示订单详情
function showOrderDetail(dept, patient) {
    alert(`您查看的订单信息：\n科室：${dept}\n服务对象：${patient}`);
    // 在实际应用中，这里应该跳转到订单详情页面
}

// 模拟通知轮播
function startNoticeCarousel() {
    const notices = [
        '欢迎使用护工助手，请完成今日上岗签到！',
        '您有一个新的培训任务需要完成',
        '本月绩效考核将于25日开始',
        '系统将于今晚23:00-24:00进行维护升级'
    ];
    
    let currentNotice = 0;
    const noticeElement = document.querySelector('.notice-item p');
    
    setInterval(() => {
        currentNotice = (currentNotice + 1) % notices.length;
        
        // 添加淡出效果
        noticeElement.style.opacity = 0;
        
        setTimeout(() => {
            noticeElement.textContent = notices[currentNotice];
            noticeElement.style.opacity = 1;
        }, 500);
    }, 5000);
}

// 显示轻提示
function showToast(message) {
    // 检查是否已有toast
    let toast = document.querySelector('.toast');
    if (toast) {
        toast.remove();
    }
    
    // 创建新的toast
    toast = document.createElement('div');
    toast.className = 'toast';
    toast.textContent = message;
    document.body.appendChild(toast);
    
    // 样式
    toast.style.position = 'fixed';
    toast.style.left = '50%';
    toast.style.bottom = '10%';
    toast.style.transform = 'translateX(-50%)';
    toast.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
    toast.style.color = 'white';
    toast.style.padding = '10px 20px';
    toast.style.borderRadius = '20px';
    toast.style.fontSize = '14px';
    toast.style.zIndex = '9999';
    toast.style.opacity = '0';
    toast.style.transition = 'opacity 0.3s';
    
    // 显示和自动隐藏
    setTimeout(() => {
        toast.style.opacity = '1';
    }, 10);
    
    setTimeout(() => {
        toast.style.opacity = '0';
        setTimeout(() => {
            toast.remove();
        }, 300);
    }, 2000);
} 